/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import type { Effect, Reducer } from 'umi';
import {
  inboundAndOutboundRecords,
  innAccountInfo,
  getWayBillSearch,
  getChargeList,
  getSustainList,
  getSmsSendSearch,
  getCloudSmsList,
  getDeliveryList,
  getVerCodeList,
  getAccountOperationLog,
} from '@/services/customerSearch';
import { message } from 'antd';
import type { RecordListType } from '../types/InnStorageRecord';
import type { InnAccountInfoData } from '../types/InnAccountInfo';
import type { RealnameBillDataType } from '../types/RealnameBill';
import type { ChargeSustainCDataType, ChargeSustainSDataType } from '../types/ChargeSustain';
import type { SmsSendSearchDataType } from '../types/SmsSendSearch';
import type { CloudSmsDataType } from '../types/CloudSmsSearch';
import type { DeliverListType, DeliveryInfoType } from '../types/DeliverySearch';
import type { VerCodeSearchListType } from '../types/VerCodeSearch';
import type { AccountOperationLog } from '../types/AccountOperationLog';
import { getRanString } from '../components/utils';

export interface CustomerSearchType {
  innStorageRecordList?: RecordListType;
  innAccountInfoData?: InnAccountInfoData;
  realnameBillData?: RealnameBillDataType;
  chargeData?: ChargeSustainCDataType;
  susTainData?: ChargeSustainSDataType;
  smsSendSearchList?: SmsSendSearchDataType[];
  cloudSmsList?: CloudSmsDataType[];
  deliveryInfo?: DeliveryInfoType;
  deliverList?: DeliverListType[];
  verCodeList?: VerCodeSearchListType[];
  AccountOperationLog?: AccountOperationLog[];
}

export interface NoticeVerifyModelType {
  namespace: string;
  state: CustomerSearchType;
  effects: {
    inboundAndOutboundRecords: Effect;
    innAccountInfo: Effect;
    getWayBillSearch: Effect;
    getChargeList: Effect;
    getSustainList: Effect;
    getSmsSendSearch: Effect;
    getCloudSmsList: Effect;
    getDeliveryList: Effect;
    getVerCodeList: Effect;
    getAccountOperationLog: Effect;
  };
  reducers: {
    save: Reducer<CustomerSearchType>;
  };
}

const Model: NoticeVerifyModelType = {
  namespace: 'customerSearch',

  state: {},

  effects: {
    *inboundAndOutboundRecords({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(inboundAndOutboundRecords, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const mixedData = Array.isArray(data?.mixed_data) ? data?.mixed_data : [];
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            innStorageRecordList: {
              recordList: mixedData.map((item: any) => ({ id: getRanString(), ...item })),
              baseinfo: [
                {
                  id: '1',
                  ...(mixedData[0] || {}),
                  ...(data?.base_info || {}),
                },
              ],
            },

            // innStorageRecordList: {
            //   ...data,
            //   baseinfo: [baseinfo],
            // },
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            innStorageRecordList: {
              baseinfo: [],
              recordList: [],
            },
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *innAccountInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(innAccountInfo, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      if (code == 0 && data.code == 0) {
        yield put({
          type: 'save',
          payload: {
            innAccountInfoData: data.data,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            innAccountInfoData: {},
          },
        });
        message.error(code == 0 ? data?.msg || msg : msg);
        __dva_reject(response);
      }
    },
    *getWayBillSearch({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWayBillSearch, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            realnameBillData: data,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            realnameBillData: {},
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getChargeList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getChargeList, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            chargeData: data,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            chargeData: {},
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getSustainList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSustainList, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            susTainData: data,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            susTainData: {},
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getSmsSendSearch({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSmsSendSearch, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const { list = [] } = Array.isArray(data) ? {} : data;
      const format_cn = (text: string) => (text == 't' ? '是' : '否');
      const format_cn_obj = {
        t: '是',
        f: '否',
      };
      const newList = list.map((item: any) => {
        // 转换数据类型
        const {
          isSendSms = '',
          isBlack = '',
          shieldSendSms = '',
          push_tao,
          has_outstorage,
          is_tao_dh,
          ...rest
        } = item;
        return {
          ...rest,
          isSendSms: format_cn(isSendSms),
          isBlack: format_cn(isBlack),
          shieldSendSms: shieldSendSms == 't' ? '否' : '是',
          push_tao: push_tao == '1' ? '是' : '否',
          has_outstorage: format_cn_obj[has_outstorage] || '',
          is_tao_dh: format_cn_obj[is_tao_dh] || '',
        };
      });
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            smsSendSearchList: newList,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            smsSendSearchList: [],
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getCloudSmsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getCloudSmsList, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const { list = [], phone = '' } = Array.isArray(data) ? {} : data;
      const newList = list.map((item: any) => ({
        ...item,
        phone,
      }));
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            cloudSmsList: newList,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            cloudSmsList: [],
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getDeliveryList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDeliveryList, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            deliverList: data.phone
              ? [
                  {
                    ...data,
                    id: '1',
                    brand: payload.brand,
                  },
                ]
              : [{ id: '1' }],
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            deliverList: [{ id: '1' }],
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getVerCodeList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getVerCodeList, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const newList = (Array.isArray(data) ? data : []).map((item, index) => ({
        ...item,
        num: index + 1 + '',
      }));
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            verCodeList: newList,
            isEmptyVerCodeList: !newList.length,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            verCodeList: [],
            isEmptyVerCodeList: false,
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getAccountOperationLog({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getAccountOperationLog, payload);
      if (!response) return;
      const { code, msg, data = [] } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            AccountOperationLog: data.length > 0 ? data : [{ content: '暂无记录' }],
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            AccountOperationLog: [],
          },
        });
        message.error(msg);
        __dva_reject(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
